package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
            // 1) 根据菜品id 或者 ,套餐id, 用户id , 口味 查询购物

           ShoppingCart shoppingCart = new ShoppingCart();
           BeanUtils.copyProperties(dto,shoppingCart);// 复制属性
           shoppingCart.setUserId(BaseContext.getCurrentId()); // 获取用户id

           List<ShoppingCart> cartList =  shoppingCartMapper.list(shoppingCart);

           // 2) 如果不存在则添加
            if(cartList==null||cartList.size()==0){
                if(dto.getDishId()!=null){
                    // 如果是菜品,查询菜品表
                    Dish dish = dishMapper.getById(dto.getDishId());
                    shoppingCart.setImage(dish.getImage());
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setAmount(dish.getPrice());
                    shoppingCart.setNumber(1); // 一件菜品
                    shoppingCart.setCreateTime(LocalDateTime.now());
                }else {
                    //如果是套餐
                    Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                    shoppingCart.setImage(setmeal.getImage());
                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setAmount(setmeal.getPrice());
                    shoppingCart.setNumber(1); // 一件菜品
                    shoppingCart.setCreateTime(LocalDateTime.now());
                }
                // 添加购物车
                shoppingCartMapper.insert(shoppingCart);
            }else {
                // 3) 如果存在修改数量
                shoppingCart= cartList.get(0); // 获取集合的第1个元素
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.updateNumber(shoppingCart);
            }
    }

    /**
     * 只根据用户id 查询
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }
    //清空购物车
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        //1) 查询量购物车中当前商品的数量
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);// 复制属性
        shoppingCart.setUserId(BaseContext.getCurrentId()); // 获取用户id
        List<ShoppingCart> cartList =  shoppingCartMapper.list(shoppingCart);
        //2) 如果数量是1 删除
        if(cartList!=null&&cartList.size()>0){
            shoppingCart= cartList.get(0);
            if(shoppingCart.getNumber()==1){
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //3) 否则数量减1
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateNumber(shoppingCart);
            }

        }

    }
}
